package com.farm.learn.utils;

import java.io.IOException;
import java.util.List;

import com.farm.category.domain.Tag;
import com.farm.core.ParameterService;
import com.farm.learn.domain.ClassHour;
import com.farm.learn.domain.ex.ClassView;
import com.farm.parameter.FarmParameterService;
import com.farm.sfile.WdapFileServiceInter;
import com.farm.sfile.domain.ex.PersistFile;
import com.farm.util.spring.BeanFactory;
import com.farm.wcp.client.WcpClient;
import com.farm.wcp.client.WcpCreaterClient;
import com.farm.wcp.client.domain.RemoteKnow;
import com.farm.wcp.client.domain.RemoteKnow.DOMTYPE;
import com.farm.wcp.client.utils.WdasJsonMap;

public class WcpSycner implements Runnable {

	private static List<WcpKnow> wcpKnows;
	private static boolean isRun = false;
	public static int allnum = 1;
	public static int cnum = 1;

	public static boolean isRunning() {
		return isRun;
	}

	public static int getAllNum() {
		return allnum;
	}

	public static int getCurrentNum() {
		return cnum;
	}

	public void run() {
		try {
			isRun = true;
			ParameterService config = FarmParameterService.getInstance();
			WcpClient wcpConfig = new WcpClient(config.getParameter("config.wcp.api.base.url") + "/api",
					config.getParameter("config.wcp.api.secret"), config.getParameter("config.wcp.api.loginname"),
					config.getParameter("config.wcp.api.password"));
			WcpCreaterClient client = new WcpCreaterClient(wcpConfig);

			WdapFileServiceInter wdapFileServiceImpl = (WdapFileServiceInter) BeanFactory
					.getBean("wdapFileServiceImpl");
			allnum = wcpKnows.size();
			cnum=0;
			for (WcpKnow know : wcpKnows) {
				cnum++;
				RemoteKnow rknow = new RemoteKnow(know.getTitle(), know.getTypeid(), know.getText());
				{
					PersistFile imgFile = wdapFileServiceImpl.getPersistFile(know.getFileid());
					if (imgFile.getFile().exists()) {
						try {
							WdasJsonMap backObj = client.addFile(imgFile.getFile(), imgFile.getName(),
									know.getFileid());
							rknow.setImgid(backObj.getString("FILEID"));
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
				rknow.setDomtype(DOMTYPE.RESOURCE_WEB);
				rknow.setResourceurl(know.getResourceurl());
				rknow.setSyncid(know.getAppid());
				rknow.setTags(know.getTags());
				client.creatKnow(rknow);
			}
		} finally {
			isRun = false;
		}
	}

	public static class WcpKnow {
		private String title;
		private String typeid;
		private String text;
		private String resourceurl;
		private String appid;
		private String tags;
		private String fileid;

		public String getFileid() {
			return fileid;
		}

		public void setFileid(String fileid) {
			this.fileid = fileid;
		}

		public String getTags() {
			return tags;
		}

		public void setTags(String tags) {
			this.tags = tags;
		}

		public String getAppid() {
			return appid;
		}

		public void setAppid(String appid) {
			this.appid = appid;
		}

		public String getTitle() {
			return title;
		}

		public String getResourceurl() {
			return resourceurl;
		}

		public void setResourceurl(String resourceurl) {
			this.resourceurl = resourceurl;
		}

		public void setTitle(String title) {
			this.title = title;
		}

		public String getTypeid() {
			return typeid;
		}

		public void setTypeid(String typeid) {
			this.typeid = typeid;
		}

		public String getText() {
			return text;
		}

		public void setText(String text) {
			this.text = text;
		}

	}

	/**
	 * 启动同步线程
	 * 
	 * @param wcpKnows
	 */
	public static void start(List<WcpKnow> knows) {
		if (!isRun) {
			wcpKnows = knows;
			(new Thread(new WcpSycner())).start();
		} else {
			System.out.println("wcpSycner is running!!!!");
		}
	}

	public static WcpKnow load(ClassView classv) {
		WcpKnow know = new WcpKnow();
		know.setTitle("课程-" + classv.getClasst().getName());
		know.setAppid(classv.getClasst().getId());
		know.setText(classv.getClasst().getShortintro());
		know.setFileid(classv.getClasst().getImgid());
		String tags = "课程";
		if (classv.getClasst().getAltime() > 0) {
			tags = tags + ",时长" + classv.getClasst().getAltime() + "小时";
		}
		for (Tag tag : classv.getTags()) {
			tags = tags + "," + tag.getName();
		}
		know.setTags(tags);
		know.setResourceurl(FarmParameterService.getInstance().getParameter("config.wcp.wlp.host")
				+ "/classweb/Pubview.do?classid=" + classv.getClasst().getId());
		String typeid = FarmParameterService.getInstance().getParameter("config.wcp.type.id");
		know.setTypeid(typeid);
		return know;
	}

	public static WcpKnow load(ClassHour hour, ClassView classv) {
		WcpKnow know = new WcpKnow();
		know.setTitle("课时-" + classv.getClasst().getName() + ":" + hour.getTitle());
		know.setAppid(hour.getId());
		know.setFileid(classv.getClasst().getImgid());
		String tags = "课时";
		if (hour.getAltime() > 0) {
			tags = tags + ",时长" + hour.getAltime() + "分";
		}
		for (Tag tag : classv.getTags()) {
			tags = tags + "," + tag.getName();
		}
		know.setTags(tags);
		know.setText(hour.getTitle() + "," + classv.getClasst().getShortintro());
		know.setResourceurl(FarmParameterService.getInstance().getParameter("config.wcp.wlp.host")
				+ "/hourweb/PubContinue.do?classid=" + hour.getClassid() + "&hourid=" + hour.getId());
		String typeid = FarmParameterService.getInstance().getParameter("config.wcp.type.id");
		know.setTypeid(typeid);
		return know;
	}
}
